//
//  DWFileViewController.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/5/19.
//

#import "DWFileViewController.h"
#import "DWFileModel.h"
#import "DWCityModel.h"
#import "DWDatabaseManager.h"
#import "DWFeedbackModel.h"

@interface DWFileViewController ()

@property (nonatomic, strong) DWFeedbackModel *model;

@end

@implementation DWFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)plistWrite:(id)sender {
}
- (IBAction)plistRead:(id)sender {
}

- (IBAction)gd:(id)sender {
    
    DWCityModel *city = [[DWCityModel alloc] init];
    city.key = @"1";
    city.cityName = @"深圳市";
    city.cityID = @"0755";
    city.lat = 104.789;
    city.lon = 22.789;
    
    NSString *document =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [document stringByAppendingPathComponent:@"city.txt"];
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:path]) {
        BOOL success = [fm createFileAtPath:path contents:nil attributes:nil];
        if (success) {
            NSLog(@"创建成功");
        }
    }
    
    NSData *cityData = [NSKeyedArchiver archivedDataWithRootObject:city requiringSecureCoding:YES error:nil];
    NSError *error = nil;
    [cityData writeToFile:path options:NSDataWritingAtomic error:&error];
    if (error) {
        NSLog(@"归档失败: %@", error);
    } else {
        NSLog(@"归档成功 %@", path);
    }
    
    //失败
    //NSURL *cityURL = [NSURL URLWithString:path];
    //[cityData writeToURL:cityURL options:NSDataWritingAtomic error:&error];
    
}
- (IBAction)jd:(id)sender {
    
    NSString *document =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [document stringByAppendingPathComponent:@"city.txt"];
    NSData *cityData = [NSData dataWithContentsOfFile:path];
    DWCityModel *city = [NSKeyedUnarchiver unarchivedObjectOfClass:[DWCityModel class] fromData:cityData error:nil];
    
    NSLog(@"%@", city);
    
}

#pragma mark - 数据库

- (IBAction)createTable:(id)sender {
    
    DWFeedbackModel *model = [[DWFeedbackModel alloc] initWithContent:@"klsdaf" phoneNumber:@"152569632584" deviceID:@"125" osVersion:@"ghjkl456789" osChannel:@"7418852789" uid:9 childId:8];
    model.identifier = @"ab";
    model.token = @"34567890-fghjkl";
    NSArray <UIImage *>*images = @[[UIImage imageNamed:@"bb_news_entry"], [UIImage imageNamed:@"bb_video_entry"]];
    NSMutableArray <NSData *>*datas = [[NSMutableArray alloc] init];
    for (UIImage *image in images) {
        NSData *data = UIImageJPEGRepresentation(image, 1);
        [datas addObject:data];
    }
    model.images = [datas copy];
    self.model = model;
    
}

- (IBAction)insert:(id)sender {
    [self.model bg_saveOrUpdate];
}

- (IBAction)update:(id)sender {
    self.model.identifier = @"iokikju";
    [self.model bg_saveOrUpdate];
}
#pragma mark - keychain

- (IBAction)keychainWrite:(id)sender {
}
- (IBAction)keychainRead:(id)sender {
}

#pragma mark - 沙盒路径

- (IBAction)document:(id)sender {
//    NSString *path = NSHomeDirectory();
//    NSLog(@"%@", path);
    NSString *document =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", document);
}
- (IBAction)tmp:(id)sender {
    NSLog(@"%@", NSTemporaryDirectory());
}
- (IBAction)cache:(id)sender {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", path);
}
- (IBAction)preference:(id)sender {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", path);
}

@end
